package com.meirengu.channel.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.meirengu.channel.service.XbbService;
import com.meirengu.utils.HttpUtil;
import com.meirengu.utils.MD5Util;
import com.meirengu.webview.common.Constants;
import com.meirengu.webview.model.KwXbbPush;
import com.meirengu.webview.model.KwXbbPushLog;
import com.meirengu.webview.service.KwThirdbackService;
import com.meirengu.webview.service.KwXbbPushLogService;
import com.meirengu.webview.service.KwXbbPushService;
import com.meirengu.webview.utils.ConfigUtil;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 新贝
 *
 * @author Mao
 * @create 2017-12-15 12:11
 */
@Service
public class XbbServiceImpl implements XbbService {

    Logger logger = LoggerFactory.getLogger(XbbServiceImpl.class);
    @Autowired
    KwThirdbackService kwThirdbackService;

    @Autowired
    private KwXbbPushService kwXbbPushService;

    @Autowired
    private KwXbbPushLogService kwXbbPushLogService;

    /**
     * @param
     * @return
     */

    public Integer applyInsurance(String policyHolderName, String mobile, String policyHolderBirth,
                                  String policyHolderSex, String fromIp, String loanAmount, String income,
                                  String hasHouse, String hasCar, String isCommercialInsurance, String userAgent) throws Exception {

        String adCode = ConfigUtil.getConfig("xbb.adcode");
        String signStr = ConfigUtil.getConfig("xbb.signstr");
        String url = ConfigUtil.getConfig("xbb.url");

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("adCode", adCode);
        jsonObject.put("policyHolderName", policyHolderName);
        jsonObject.put("mobile", mobile);
        jsonObject.put("policyHolderBirth", policyHolderBirth);
        jsonObject.put("policyHolderSex", policyHolderSex);
        jsonObject.put("insuredPersonName", policyHolderName);
        jsonObject.put("insuredPersonBirth", policyHolderBirth);
        jsonObject.put("insuredPersonSex", policyHolderSex);
        jsonObject.put("relation", "SELF");
        jsonObject.put("activityConfigNum", "0");
        jsonObject.put("sign", MD5Util.get32MD5(adCode + signStr + mobile));
        jsonObject.put("fromIp", fromIp);
        jsonObject.put("userAgent", userAgent);
        JSONObject tagJson = new JSONObject();
        //tagJson.put("loanAmount",loanAmount);
        tagJson.put("income", income);
        //tagJson.put("hasHouse",hasHouse);
        //tagJson.put("hasCar",hasCar);
        tagJson.put("isCommercialInsurance", isCommercialInsurance);
        jsonObject.put("tag", tagJson);


        //接口调用信息表
        KwXbbPush kwXbbPush = new KwXbbPush();
        kwXbbPush.setInterfaceCode("xbb");
        kwXbbPush.setInterfaceName("新贝");
        kwXbbPush.setMobile(mobile);
        kwXbbPush.setCode(Constants.BUSINESS_STATUS_FAIL);


        KwXbbPushLog kwXbbPushLog = new KwXbbPushLog();
        kwXbbPushLog.setInterfaceCode("xbb");
        kwXbbPushLog.setInterfaceName("新贝");
        kwXbbPushLog.setMobile(mobile);
        kwXbbPushLog.setSendUrl(url.toString());
        kwXbbPushLog.setParams(jsonObject.toString());


        HttpUtil.HttpResult hr = null;
        try {
            hr = HttpUtil.doPost(url, jsonObject.toString());
            int code = hr.getStatusCode();
            if (code == 200) {
                kwXbbPushLog.setBackUrl(hr.toString());
                kwXbbPushLog.setHttpCode(200);
                kwXbbPushLog.setHttpContent(hr.toString());

                logger.info("==XbbServiceImpl.applyLoan request success. result:{}", hr.toString());
                String content = hr.getContent();
                JSONObject returnMsg = JSONObject.parseObject(content);
                String rCode = returnMsg.getString("status");
                String rMsg = returnMsg.getString("detailMessage");
                if ("SUCCEEDED".equals(rCode)) {
                    kwXbbPush.setCode(Constants.BUSINESS_STATUS_SUCCESS);
                    kwXbbPush.setMessage(rMsg);
                    logger.info("==XbbServiceImpl.applyLoan business success");
                } else {
                    kwXbbPush.setCode(Constants.BUSINESS_STATUS_OTHER);
                    kwXbbPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    logger.warn("==XbbServiceImpl.applyLoan business fail");
                }
            } else {
                kwXbbPushLog.setBackUrl(hr.toString());
                kwXbbPushLog.setHttpCode(code);
                kwXbbPushLog.setHttpContent(hr.toString());
                logger.warn("==XbbServiceImpl.applyLoan request fail. code:{}, result:{}", code, hr.toString());
            }
        } catch (Exception e) {
            kwXbbPushLog.setHttpCode(408);
            kwXbbPushLog.setHttpContent("Exception");
            e.printStackTrace();
            logger.error(ExceptionUtils.getFullStackTrace(e));
        } finally {
            kwXbbPushService.insert(kwXbbPush);
            kwXbbPushLog.setPushId(kwXbbPush.getId());
            kwXbbPushLogService.insert(kwXbbPushLog);
            return kwXbbPush.getCode();
        }

    }

}
